package com.learn.project2.config.aspectj;


import com.learn.project2.bean.MarketAdmin;
import com.learn.project2.bean.MarketLog;
import com.learn.project2.bean.MarketOrder;
import com.learn.project2.mapper.MarketLogMapper;
import com.learn.project2.mapper.MarketOrderMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * @description:
 * @author zhizhuke
 * @date 2022-01-09 16-26 
 * @version 1.0
 */
@Aspect
@Component
public class AutoControllerAspect {

    @Autowired
    MarketLogMapper logMapper;
    @Autowired
    MarketOrderMapper orderMapper;


    @Pointcut("execution(* com.learn.project2.controller.admin.AuthController.login(..))")
    public void autoLogin() {//只需要这个方法的方法名
    }
    @Pointcut("execution(* com.learn.project2.controller.admin.AuthController.logout(..))")
    public void autoLogout() {//只需要这个方法的方法名
    }

    @Transactional
    @Around("autoLogin()")
    public Object login(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        //在删除用户之前，插入一条操作日志，默认是失败的
        Object[] args = proceedingJoinPoint.getArgs();
        HttpServletRequest request = ((HttpServletRequest) args[1]);
        Map map = ((Map) args[0]);
        String username = (String) map.get("username");

        String ip = request.getRemoteAddr();
        Date date = new Date();

        MarketLog marketLog = new MarketLog(null,username,ip,1,"登录",false,null,"账户名或密码不正确", date, date,false);

        logMapper.insertSelective(marketLog);

        //执行controller的delete方法
        Object proceed = proceedingJoinPoint.proceed();


        //修改为成功
        marketLog.setStatus(true);
        logMapper.updateByPrimaryKeySelective(marketLog);


        return proceed;
    }

    @Transactional
    @Around("autoLogout()")
    public Object logout(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        //在删除用户之前，插入一条操作日志，默认是失败的
        Object[] args = proceedingJoinPoint.getArgs();
        HttpServletRequest request = ((HttpServletRequest) args[0]);
        Subject subject = SecurityUtils.getSubject();
        
        MarketAdmin admin = (MarketAdmin) subject.getPrincipal();

        String ip = request.getRemoteAddr();
        Date date = new Date();

        MarketLog marketLog = new MarketLog(null,admin.getUsername(),ip,1,"退出",false,null,null, date, date,false);

        logMapper.insertSelective(marketLog);

        //执行controller的delete方法
        Object proceed = proceedingJoinPoint.proceed();


        //修改为成功
        marketLog.setStatus(true);
        logMapper.updateByPrimaryKeySelective(marketLog);


        return proceed;
    }





}
